package com.zzzy.doc.controller.mydoc;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.dao.entity.DocAttachment;
import com.zzzy.doc.service.DocAttachmentService;
import com.zzzy.doc.vo.document.attachment.DocAttachmentAddReq;
import com.zzzy.doc.vo.document.attachment.DocAttachmentPageReq;
import com.zzzy.doc.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "我的公文-附件", description = "doc105")
@Validated
@RestController
@RequestMapping("/doc/myDocAttachment")
public class MyDocAttachmentController {

    @Resource
    private DocAttachmentService docAttachmentService;

    @Operation(summary = "分页列表")
    @GetMapping(value = "/page")
    @PreAuthorize("@pvs.hasPermission('doc:my:page')")
    public AjaxResult<PageResult<DocAttachment>> page(@Valid @ParameterObject DocAttachmentPageReq queryReq) {
        PageResult<DocAttachment> docAttachmentPage = docAttachmentService.getDocAttachmentPage(queryReq);
        return AjaxResult.success(docAttachmentPage);
    }

    @PostMapping("/add")
    @Operation(summary = "新增公文附件")
    @Parameter(name = "file", description = "上传的附件")
    @PreAuthorize("@pvs.hasPermission('doc:my:page')")
    public AjaxResult add(MultipartFile file, @Valid @ParameterObject DocAttachmentAddReq addReq) {
        docAttachmentService.addDocAttachment(addReq);
        return AjaxResult.success();
    }

    @GetMapping(value = "/getPageOfficeFile/{id}")
    @Operation(summary = "打开文件")
    @Parameter(name = "id", description = "公文附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:my:page')")
    public AjaxResult<OpenOfficeFileRes> getPageOfficeFile(@NotNull(message = "公文附件ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/myDocAttachment/getAttachmentFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = docAttachmentService.getPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getAttachmentFileStream/{id}")
    @Operation(summary = "获取文件流")
    @Parameter(name = "id", description = "公文附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:my:page')")
    public void getAttachmentFileStream(@NotNull(message = "公文附件ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocAttachment docAttachment = docAttachmentService.getDocAttachmentById(id);
        FileUtil.outputFileStream(response, docAttachment.getFileBlob(), docAttachment.getName());
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除公文附件")
    @Parameter(name = "id", description = "公文附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:my:page')")
    public AjaxResult delete(@NotNull(message = "公文附件ID不能为空") @PathVariable("id") Long id) {
        docAttachmentService.deleteById(id);
        return AjaxResult.success();
    }
}
